package com.ecarx.engineermode.controller;

import android.os.SystemClock;
import android.util.Log;

import com.ecarx.engineermode.tab.Model.TabModel;

public class ProtectedFeatureController {


    private static final int ACCESS_COUNT = 10;
    private static final long INTERVAL_MAX = 500;

    private long mRequestLastTime = 0;
    private int mRequestCount = 0;

    private Callback mCallback;

    private boolean isDone;

    public void init(Callback callback) {
        mCallback = callback;
    }

    public void request() {
        //非SOP
        if (!TabModel.SOP) {
            return;
        }

        if (isDone) {
            return;
        }

        //点击两次间隔不能大于 INTERVAL_MAX，超过则重置点击次数
        long realtime = SystemClock.elapsedRealtime();
        Log.d("ProtectedFeatureController", "request: " + mRequestCount);
        if (Math.abs(realtime - mRequestLastTime) > INTERVAL_MAX) {
            mRequestCount = 1;
        } else {
            mRequestCount++;
            if (mRequestCount >= ACCESS_COUNT) {
                isDone = true;
                mCallback.onAccessCallback();
            }
        }
        mRequestLastTime = realtime;
    }

    public interface Callback {
        void onAccessCallback();
    }

}
